//
//  MyAssetsController.m
//  FinancialProduct
//  资产总览
//  Created by Stella on 2017/8/27.
//  Copyright © 2017年 maq. All rights reserved.
//
//#import "UINavigationController+FDFullscreenPopGesture.h"
#import "UIColor+NN.h"
#import "YTLPieView.h"
#import "Header.h"
#import "CategoryCell.h"
#import "UserModel.h"
#import "NetworkConfig.h"
#import "CKPuslichTool.h"
#import "HandleNetProduct.h"
#import "MyAssetsController.h"
#import "ReminderWebController.h"

@interface MyAssetsController ()
@property (weak, nonatomic) IBOutlet UIView *pieView;
@property (weak, nonatomic) IBOutlet UIView *TopView;
@property (weak, nonatomic) IBOutlet UITableView *myTable;
@property (weak, nonatomic) IBOutlet UIButton *ChangBt;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;

@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UILabel *label4;
@property (weak, nonatomic) IBOutlet UIButton *LeftBt;
@property (weak, nonatomic) IBOutlet UILabel *LeftLabel;
@property (weak, nonatomic) IBOutlet UIButton *RightBt;
@property (weak, nonatomic) IBOutlet UILabel *RightLabel;
@property (weak, nonatomic) IBOutlet UIView *moneyView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moneyBtHeight;
@property (strong,nonatomic)NSArray *DataArray;
@property(strong,nonatomic)NSString *ProductType;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeight;
@property (weak, nonatomic) IBOutlet UIView *clearView;
@property(strong,nonatomic)NSDictionary *AccountInfo;
@end

@implementation MyAssetsController

- (void)viewDidLoad {
    [super viewDidLoad];

    _ProductType=@"";
    if ([_isOnlyOne isEqualToString:@"ONE"]) {
        self.moneyBtHeight.constant=0;
        self.moneyView.hidden=YES;
        self.topViewHeight.constant=400-50;
    }else{
        self.moneyBtHeight.constant=50;
        self.moneyView.hidden=NO;
        self.topViewHeight.constant=400;
        
        if ([_typeStr isEqualToString:@"RMB"]) {
            self.LeftBt.selected=YES;
            self.RightBt.selected=NO;
            self.LeftLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
            self.RightLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
            
        }else{
            self.LeftBt.selected=NO;
            self.RightBt.selected=YES;
            self.RightLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
            self.LeftLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
          
        }
        
    }
    
    self.DataArray=@[@"全部资产",@"现金管理",@"固收/类固收",@"一级市场",@"二级市场"];
    self.ChangBt.selected=NO;
    self.categoryLabel.text=@"全部资产";
    self.title=@"资产总览";
    //F5DF2F  5BBAF0  EE6A66 497B90
//    [self initMyPieView];

    [self getUserData];
    // Do any additional setup after loading the view.
}
-(void)getUserData{
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    NSDictionary *params=@{@"AAAMethodName":@"InvestOverviews",@"CustomerID":@(mid),
                           @"CurrencyType":_typeStr,
                           @"ProductTypeLevel":self.ProductType};
    [HandleNetProduct getListforParams:params Success:^(NSArray *data) {
        /*
         * Cash 现金管理类
         * Fixed 固收类固收
         * PrimaryMarket 一级市场
         * SecondaryMarket 二级市场
         * TotalAmount 投资总金额
         * RedeemAmount 已分配或赎回金额
         * SurvivingScale 存续规模
         * RedeemEarnings 已分配/赎回收益
         */
        if (data.count>0) {
            NSDictionary *dicct=data[0];
            self.AccountInfo=dicct;
            [self getUseDataString];
            [self reloadMyLabelText];
        }
        
    } Failure:^(NSString *errorInfo) {
        
    }];
    
   
}
-(void)getUserData2{
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    NSDictionary *params=@{@"AAAMethodName":@"InvestOverviews",@"CustomerID":@(mid),
                           @"CurrencyType":_typeStr,
                           @"ProductTypeLevel":self.ProductType};
    [HandleNetProduct getListforParams:params Success:^(NSArray *data) {
     
        if (data.count>0) {
            NSDictionary *dicct=data[0];
            self.AccountInfo=dicct;
            [self reloadMyLabelText];
        }
        
    } Failure:^(NSString *errorInfo) {
        
    }];
    
    
}
-(void)getUseDataString{
    for (UIView *vc in self.pieView.subviews) {
        [vc removeFromSuperview];
    }
    float Cash=[self.AccountInfo[@"Cash"] floatValue];
    float Fixed=[self.AccountInfo[@"Fixed"] floatValue];
    float PrimaryMarket=[self.AccountInfo[@"PrimaryMarket"] floatValue];
    float SecondaryMarket=[self.AccountInfo[@"SecondaryMarket"] floatValue];
    float totalMoney=Cash+Fixed+PrimaryMarket+SecondaryMarket;
    NSString *str1=[NSString stringWithFormat:@"%@(%.1f%%)",self.AccountInfo[@"Fixed"],Fixed/totalMoney*100];
    NSString *str2=[NSString stringWithFormat:@"%@(%.1f%%)",self.AccountInfo[@"Cash"],Cash/totalMoney*100];
    NSString *str3=[NSString stringWithFormat:@"%@(%.1f%%)",self.AccountInfo[@"PrimaryMarket"],PrimaryMarket/totalMoney*100];
    NSString *str4=[NSString stringWithFormat:@"%@(%.1f%%)",self.AccountInfo[@"SecondaryMarket"],SecondaryMarket/totalMoney*100];
    NSMutableArray *dataAa=[NSMutableArray arrayWithCapacity:0];
    NSMutableArray *upTextAa=[NSMutableArray arrayWithCapacity:0];
    NSMutableArray *downtextAa=[NSMutableArray arrayWithCapacity:0];
    NSMutableArray *colortAa=[NSMutableArray arrayWithCapacity:0];
    if (Fixed>0) {
        [dataAa addObject:@(Fixed/totalMoney*10)];
        [upTextAa addObject:str1];
        [downtextAa addObject:@"固收/类固收"];
        [colortAa addObject:[UIColor colorWithHexString:@"F5DF2F"]];
    }
    if (Cash>0) {
        [dataAa addObject:@(Cash/totalMoney*10)];
        [upTextAa addObject:str2];
        [downtextAa addObject:@"现金管理"];
        [colortAa addObject:[UIColor colorWithHexString:@"5BBAF0"]];
    }
    if (PrimaryMarket>0) {
        [dataAa addObject:@(PrimaryMarket/totalMoney*10)];
        [upTextAa addObject:str3];
        [downtextAa addObject:@"一级市场"];
        [colortAa addObject:[UIColor colorWithHexString:@"EE6A66"]];
    }
    if (SecondaryMarket>0) {
        [dataAa addObject:@(SecondaryMarket/totalMoney*10)];
        [upTextAa addObject:str4];
        [downtextAa addObject:@"二级市场"];
        [colortAa addObject:[UIColor colorWithHexString:@"497B90"]];
    }
    
    NSString *toto=self.AccountInfo[@"SurvivingScale"];
//    [NSString stringWithFormat:@"%.2f",[self.AccountInfo[@"SurvivingScale"] floatValue]];
    YTLPieView *pieView1 = [[YTLPieView alloc] initWithFrame:CGRectMake(0,0, self.pieView.bounds.size.width, 300)
                                                   dataItems:dataAa
                                                  colorItems:colortAa
                                                 upTextItems:upTextAa
                                               downTextItems:downtextAa
                                                  centerText:toto];
    
    [self.pieView addSubview:pieView1];
    
}
-(void)reloadMyLabelText{
    /***  * TotalAmount 投资总金额
     * RedeemAmount 已分配或赎回金额
     * SurvivingScale 存续规模
     * RedeemEarnings 已分配/赎回收益**/
    self.label1.text=self.AccountInfo[@"TotalAmount"];
    self.label2.text=self.AccountInfo[@"RedeemAmount"];
    self.label3.text=self.AccountInfo[@"RedeemEarnings"];
    self.label4.text=self.AccountInfo[@"SurvivingScale"];
    
    
}
//-(void)initMyPieView{
//    YTLPieView *pieView1 = [[YTLPieView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 230)
//                                                   dataItems:@[@2.5,@2.5,@2.5,@2.5]
//                                                  colorItems:@[[UIColor colorWithHexString:@"F5DF2F"],
//                                                               [UIColor colorWithHexString:@"5BBAF0"],
//                                                               [UIColor colorWithHexString:@"EE6A66"],
//                                                               [UIColor colorWithHexString:@"497B90"]]
//                                                 upTextItems:@[@"826.59(21.3%)",@"826.59(21.3%)",@"826.59(21.3%)",@"826.59(21.3%)"]
//                                               downTextItems:@[@"固收/类固收",@"现金管理",@"一级市场",@"二级市场"]
//                                                  centerText:@"6876.88"];
//    
//    [self.pieView addSubview:pieView1];
//}
- (IBAction)changMoneyUnit:(UIButton *)sender {

    if (sender.tag==0) {
        self.LeftBt.selected=YES;
        self.RightBt.selected=NO;
        self.LeftLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.RightLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.typeStr=@"RMB";
    }else{
        self.LeftBt.selected=NO;
        self.RightBt.selected=YES;
        self.RightLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.LeftLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        self.typeStr=@"USD";
    }
    for (UIView *vc in self.pieView.subviews) {
        [vc removeFromSuperview];
    }
    [self getUserData];
}
- (IBAction)chooseCategory:(UIButton *)sender {
    sender.selected=!sender.selected;
    self.myTable.hidden=!self.myTable.hidden;
}

- (IBAction)goReminder:(id)sender {
    NSString *name;
    if ([self.ProductType isEqualToString:@""]) {
        name = @"全部资产";
    }else if (([self.ProductType isEqualToString:@"A"])){
        name = @"现金管理类";
    }else if (([self.ProductType isEqualToString:@"B"])){
        name = @"固收";
    }else if (([self.ProductType isEqualToString:@"C"])){
        name = @"一级市场";
    }else if (([self.ProductType isEqualToString:@"D"])){
        name = @"二级市场";
    }
    [HandleNetProduct GetReminderUrlWith:name userId:[UserModel getUser].CustomerID Success:^(NSDictionary *data) {
        NSLog(@"goReminder===%@",data);
        ReminderWebController *vc = (ReminderWebController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"ReminderWebController"];
        vc.filePath = data[@"path"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
        [self presentViewController:vc animated:YES completion:nil];
    } Failure:^(NSString *errorInfo) {
        
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50
    ;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.DataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CategoryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CategoryCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.NameLabel.text=self.DataArray[indexPath.row];
    if ([self.categoryLabel.text isEqualToString:self.DataArray[indexPath.row]]) {
        cell.mImage.hidden=NO;
    }else{
        cell.mImage.hidden=YES;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.myTable.hidden=!self.myTable.hidden;
    self.ChangBt.selected=NO;
    self.categoryLabel.text=self.DataArray[indexPath.row];
    switch (indexPath.row) {
        case 0:
        {
            self.ProductType=@"";
        }
            break;
        case 1:
        {
            self.ProductType=@"A";
        }
            break;
        case 2:
        {
            self.ProductType=@"B";
        }
            break;
        case 3:
        {
            self.ProductType=@"C";
        }
            break;
        case 4:
        {
            self.ProductType=@"D";
        }
            break;
        default:
            break;
    }
    [tableView reloadData];
    [self getUserData2];
//    [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
